//
//  SearchViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/1/25.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "SearchViewController.h"
#import "SingleChoseMenu.h"
#import "ShoppingProductListViewController.h"
#import "StoresListViewController.h"
@interface SearchViewController ()<UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UITextField *searchNameField;//搜索内容
@property (strong, nonatomic) IBOutlet UIView *searchView;
@property(nonatomic,strong)NSString * searchType;//搜索类型
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewLayout];
    [self getHotSearch];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout: self.scroView];
    _scroView.contentSize = flexibleSize(CGSizeMake(0, 503), NO);
    [self adaptiveViewLayout:self.searchView];
    self.searchView.layer.masksToBounds = YES;
    self.searchView.layer.cornerRadius = 17;
    self.searchView.layer.borderWidth = 1;
    self.searchView.layer.borderColor = kGetColor(230, 230, 230).CGColor;
    
   
    NSArray * array = @[@"商品",@"品牌",@"店铺"];
    ScroTitleView * scroTitle = [[ScroTitleView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 50, 35), NO) Titles:array complect:^(NSInteger index) {
        _searchType = array[index];
    }];
    [self.searchView addSubview:scroTitle];

    _searchNameField.delegate = self;
}

- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}

- (IBAction)cancelSearch:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}


#pragma mark  获取热门搜索--=========================================
-(void)getHotSearch{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryAppGoodsHotkeyword withHttpType:@"POST" withTag:SX_SX_queryAppGoodsHotkeyword];
}
#pragma   mark---- textField
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([_searchType isEqualToString:@"店铺"]||[_searchType isEqualToString:@"品牌"]) {
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
        
        StoresListViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"StoresList"];
        page.searchStr = textField.text;
        [self.navigationController   pushViewController:page animated:YES];
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        return NO;
    }else if ([_searchType isEqualToString:@"商品"]){
        
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
        ShoppingProductListViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ShoppingProductList"];
        page.goodsNameStr = textField.text;
        [self.navigationController   pushViewController:page animated:YES];
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        return NO;
    }
    
    return YES;
}
#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag == SX_SX_queryAppGoodsHotkeyword){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSArray * array = database[@"result"];
            NSMutableArray * titles = [NSMutableArray array];
            for (NSDictionary * dic in array) {
                [titles addObject:dic[@"mghkName"]];
            }
            
            UIColor * dcolor = [UIColor colorWithRed:0.008 green:0.102 blue:0.141 alpha:1.000] ;
            UIColor * redColor = [UIColor redColor];
            NSMutableArray * colorArray = [NSMutableArray array];
            for (int i = 0; i<titles.count; i++) {
                if (i==3||i==8) {
                    [colorArray addObject:redColor];
                }else{
                    [colorArray addObject:dcolor];
                }
            }
            SingleChoseMenu * choseView = [[SingleChoseMenu alloc]initWithFrame:flexibleFrame(CGRectMake(0, 49, 320, 230), NO) titles:titles XInterval:12 YInterval:12 defaultColor:[UIColor colorWithRed:0.008 green:0.102 blue:0.141 alpha:1.000] colors:colorArray cancleSelect:(BOOL)NO complect:^(NSInteger index) {
                UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
                ShoppingProductListViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ShoppingProductList"];
                page.goodsNameStr = titles[index];
                [self.navigationController   pushViewController:page animated:YES];
                [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
            }];
            [self.scroView addSubview:choseView];
        }
        
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
